package com.cl.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.blog.entity.BTag;
import com.cl.blog.entity.BType;
import com.cl.blog.exception.WebException;
import com.cl.blog.service.BTagService;
import com.cl.blog.mapper.BTagMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author object
 * @description 针对表【b_tag】的数据库操作Service实现
 * @createDate 2024-03-14 17:13:59
 */
@Service
public class BTagServiceImpl extends ServiceImpl<BTagMapper, BTag>
		implements BTagService {

	@Override
	public boolean saveName(String names) {

		// 数据处理
		String[] split = names.split(",");
		List<String> list = Arrays.asList(split);
		System.out.println("初始字符串-----" + split);
		System.out.println("处理的字符串-----" + list);
		List<String> exist = new ArrayList<>();
		List<BTag> bTags = new ArrayList<>();
		for (String s : list) {
			BTag bTag = this.getOne(new QueryWrapper<BTag>().eq("name", s));
			if (bTag != null) {
				exist.add(bTag.getName());
			} else {
				BTag bTag1 = new BTag();
				bTag1.setName(s);
				bTag1.setCreateTime(LocalDateTime.now());
				bTags.add(bTag1);
			}
		}
		if (bTags.size() <= 0) {
			throw new WebException("抱歉，当前添加数据为空，可能是当前新增分类已存在");
		}
		boolean b = this.saveBatch(bTags);
		if (!b) {
			throw new WebException("新增失败");
		}


		return true;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateBatchType(List<Integer> ids, List<String> names) {
/**
 * bTypes 是修改的id，bTypeNames是修改的名称，一一对应
 */
		List<BTag> list = new ArrayList<>();
		System.out.println("获得的数据id-----" + ids);
		System.out.println("获得的数据Name-----" + names);
		for (int i = 0; i < ids.size(); i++) {
			BTag bTag = new BTag();
			bTag.setId(ids.get(i));
			bTag.setName(names.get(i));

			bTag.setUpdateTime(LocalDateTime.now());
			list.add(bTag);
		}


		return this.updateBatchById(list);

	}
}




